//
//  HMSportCameraVC.m
//  BlackHorseLine
//
//  Created by made on 17/3/14.
//  Copyright © 2017年 made. All rights reserved.
//

#import "HMSportCameraVC.h"
#import "HMCustomCamera.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>

//腾讯开放平台（对应QQ和QQ空间）SDK头文件
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>

//微信SDK头文件
#import "WXApi.h"

//新浪微博SDK头文件
#import "WeiboSDK.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>

@interface HMSportCameraVC ()
//左边快门视图,右边与父视图左边对齐的约束(优先级1000)
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *leftViewContraint;
//右边快门视图,左边与父视图右边对齐的约束(优先级1000)
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *rightViewContraint;
//相机预览视图
@property (weak, nonatomic) IBOutlet UIView *preview;
//自定义相机
@property(nonatomic,strong)HMCustomCamera *camera;
//拍照按钮
@property (weak, nonatomic) IBOutlet UIButton *captureButton;
//切换摄像头按钮
@property (weak, nonatomic) IBOutlet UIButton *switchCameraButton;
//分享按钮
@property (weak, nonatomic) IBOutlet UIButton *shareButton;
//水印照片
@property (weak, nonatomic) IBOutlet UIImageView *waterImage;
//水印标签
@property (weak, nonatomic) IBOutlet UILabel *waterLabel;
///分享图片
@property(nonatomic,strong)UIImage *shareImage;

@end

@implementation HMSportCameraVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //创建自定义相机
    self.camera = [[HMCustomCamera alloc] initWithPreview:self.preview];
    //注册水平照片绘制通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(drawWaterImage:) name:kDidBenginDrawWaterImageNotification object:nil];
}


- (void)dealloc
{
    //移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kDidBenginDrawWaterImageNotification object:nil];
}


#pragma mark - 绘制水印照片
//绘制水印照片一定要在上下文环境中
- (void)drawWaterImage:(NSNotification *)notification
{
    //绘制图片(记住:绘制图片的方法是drawInRect(属于UIImage以及富文本的方法) ,而不是drawRect(属于UIView的方法))
    [self.waterImage.image drawInRect:self.waterImage.frame];
    [self.waterLabel.attributedText drawInRect:self.waterLabel.frame];

}

//隐藏电池栏,当前控制器
- (BOOL)prefersStatusBarHidden
{
    return YES;
}


#pragma mark - 切换摄像头按钮
- (IBAction)switchCaremaButtonClick:(id)sender {
    [self.camera switchCamera];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - 分享按钮的处理
- (IBAction)shareButtonClick:(id)sender {
    //1.创建分享参数
    NSArray* imageArray = @[self.shareImage];
    if (imageArray) {
        
        NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
        [shareParams SSDKSetupShareParamsByText:@"黑马行，不错哟"
                                         images:imageArray
                                            url:[NSURL URLWithString:@"http://heima.itcast.com"]
                                          title:@"黑马程序员"
                                           type:SSDKContentTypeAuto];
        //2、分享（可以弹出我们的分享菜单和编辑界面）
        [ShareSDK showShareActionSheet:nil
                                 items:nil
                           shareParams:shareParams
                   onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                       
                       switch (state) {
                           case SSDKResponseStateSuccess:
                           {
                           }
                           case SSDKResponseStateFail:
                           {
                           }
                           default:
                               break;
                       }
                   }];
    }
    
}

#pragma mark - 快门按钮
- (IBAction)captureButtonClick:(id)sender
{
    //拍照动画思路
    //1.如果按钮标题没有"✓",此时可以拍摄,并且显示切换摄像头按钮
    //2.点击按钮时,拍照按钮翻转动画,切换摄像头翻转动画
    //3.翻转动画完成后,按钮标题出现"✓",此预览视图静止状态,而且无法切换摄像头,点击分享按钮则分享图片
    
    //1.获取按钮标题
    NSString *string = self.captureButton.currentTitle;
    if ([string isEqualToString:@"✓"])//如果有文本
    {
        //翻转隐藏分享按钮,显示切换摄像头按钮
        [self turnButtonAnimationWithShareButton:NO];
        
        //此时点击拍照按钮只有动画,不拍照
        return;
    }
    else
    {
        //翻转显示分享按钮,隐藏切换摄像头按钮
        [self turnButtonAnimationWithShareButton:YES];
    }

    //拍照
    [self.camera captureImageCompletion:^(UIImage *image) {
        NSLog(@"拍照完成");
        self.shareImage = image;
    } SaveAlbumBlock:^(NSError *error) {
        if (error == nil)
        {
            NSLog(@"保存到系统相册成功");
        }
    }];
    //先关闭动画
    [self preViewAnimation:YES];
    
    //0.6秒之后打开动画
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self preViewAnimation:NO];
    });
}




#pragma mark -  翻转按钮动画
// @param isShare 是否翻转成分享按钮(拍照按钮显示✅,并且预览视图静止)
- (void)turnButtonAnimationWithShareButton:(BOOL)isShare
{
    if (isShare)//显示分享按钮
    {
        
        //1.切换摄像头按钮翻转,使用UIView的转场动画  options:转场类型
        [UIView transitionWithView:self.switchCameraButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
            //这里可以对视图做其他操作
            self.switchCameraButton.hidden = YES;
        } completion:^(BOOL finished) {
            
        }];
        
        //2.让分享按钮跟着切换一起翻转,可以解决完成之后瞬间切换图片的卡顿问题
        [UIView transitionWithView:self.shareButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
            //这里可以对视图做其他操作
            self.shareButton.hidden = NO;
        } completion:^(BOOL finished) {
            
        }];
        
        //3.拍照按钮翻转,使用UIView的转场动画  options:转场类型
        [UIView transitionWithView:self.captureButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
            //这里可以对视图做其他操作
        } completion:^(BOOL finished) {
            //翻转完成之后,显示✓,停止会话
            [self.captureButton setTitle:@"✓" forState:UIControlStateNormal];
            [self.camera stop];
        }];
        
        
    }
    else//隐藏分享按钮
    {
        //1.分享按钮翻转,使用UIView的转场动画  options:转场类型
        [UIView transitionWithView:self.shareButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
            //这里可以对视图做其他操作
            self.shareButton.hidden = YES;
        } completion:^(BOOL finished) {
            
            
        }];
        
        //2.让切换按钮跟着分享一起翻转,可以解决完成之后瞬间切换图片的卡顿问题
        [UIView transitionWithView:self.switchCameraButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
            //这里可以对视图做其他操作
            self.switchCameraButton.hidden = NO;
        } completion:^(BOOL finished) {
            
        }];
        
        //3.拍照按钮翻转,使用UIView的转场动画  options:转场类型
        [UIView transitionWithView:self.captureButton duration:1 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
            //这里可以对视图做其他操作
        } completion:^(BOOL finished) {
            //翻转完成之后,隐藏✓,此时拍摄会话开始工作
            [self.captureButton setTitle:@"" forState:UIControlStateNormal];
            
            [self.camera start];
            
        }];
    }
}



#pragma mark - 快门动画
- (void)preViewAnimation:(BOOL)isFold
{
    //约束安装动画本质上其实就是让优先级高的约束生效和实现来实现动画
    if (isFold == YES)
    {
        //关闭快门动画,实际上就是让优先级为1000的约束失效,此时storyboard就会加载优先级为999的约束
        [NSLayoutConstraint deactivateConstraints:@[self.leftViewContraint,self.rightViewContraint]];
    }
    else
    {
        //打开快门动画,实际就是让优先级为1000约束生效,此时999优先级的约束就会失效
        [NSLayoutConstraint activateConstraints:@[self.leftViewContraint,self.rightViewContraint]];
    }
    
    //AutoLayout动画本质上是将更新约束的方法放入到动画中
    [UIView animateWithDuration:0.5 animations:^{
        //更新布局
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
    }];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
